package com.lzy.system.organization.controller;

import com.lzy.conf.JSONResult;
import com.lzy.system.organization.dto.SysUserDTO;
import com.lzy.system.organization.entity.SysUser;
import com.lzy.system.organization.entity.security.entity.SecurityUser;
import com.lzy.system.organization.service.ISysUserService;
import com.lzy.utils.ValidList;
import com.lzy.utils.validator.group.ServiceInsert;
import com.lzy.utils.validator.group.ServiceUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户信息管理
 * </p>
 *
 * @author lcc
 * @since 2022-08-13
 */
@RestController
@RequestMapping("/organization/sysUser")
@Api(tags = "用户管理")
@Slf4j
public class SysUserController {
    ISysUserService iSysUserService;

    @Autowired
    public SysUserController(ISysUserService iSysUserService) {
        this.iSysUserService = iSysUserService;
    }

    @ApiOperation("退出登录")
    @RequestMapping(value = "exitLogin", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONResult exitLogin(HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization").replace("lzy ", "");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        SecurityUser securityUser = (SecurityUser)authentication.getPrincipal();
        iSysUserService.exitLogin( securityUser);
        return JSONResult.ok();
    }

    @ApiOperation("添加用户")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public JSONResult save(@ApiParam("用户信息") @RequestBody @Validated(value = {ServiceInsert.class}) ValidList<SysUserDTO> sysUserDTOs) throws Exception {
        iSysUserService.add(sysUserDTOs);
        return JSONResult.ok();
    }

    @ApiOperation("查询用户信息")
    @RequestMapping(value = "getUserByUserId", method = RequestMethod.GET)
    public JSONResult getUserByUserId(SysUser sysUser) throws Exception {
        return JSONResult.ok(sysUser);
    }

    @ApiOperation("修改用户")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public JSONResult update(
            @ApiParam("用户信息") @RequestBody @Validated(value = {ServiceUpdate.class}) ValidList<SysUserDTO> sysUserDTOs
    ) throws Exception {
        return JSONResult.ok(iSysUserService.update(sysUserDTOs));
    }

    @ApiOperation("删除用户")
    @RequestMapping(value = "del", method = RequestMethod.POST)
    public JSONResult del(
            @ApiParam("用户唯一标识，多个,逗号间隔") @RequestParam(name = "ids", required = true) String ids
    ) throws Exception {
        return JSONResult.ok(iSysUserService.del(ids));
    }

    @ApiOperation("查询用户列表")
    @RequestMapping(value = "getList", method = RequestMethod.POST)
    public JSONResult getList(
            @ApiParam("用户名") @RequestParam(name = "username", required = false, defaultValue = "") String username,
            @ApiParam("昵称") @RequestParam(name = "nickname", required = false, defaultValue = "") String nickname,
            @ApiParam("每页显示行数") @RequestParam(name = "limit", required = true, defaultValue = "5") int limit,
            @ApiParam("当前页数") @RequestParam(name = "page", required = true, defaultValue = "1") int page
    ) throws Exception {
        return JSONResult.ok(iSysUserService.getList(username, nickname, limit, page));
    }

    @ApiOperation("修改密码")
    @RequestMapping(value = "updatePwd", method = RequestMethod.POST)
    public JSONResult updatePwd(
            @ApiParam("旧密码") @RequestParam(name = "pwd_old") String pwd_old,
            @ApiParam("新密码") @RequestParam(name = "pwd_new") String pwd_new,
            @ApiParam("确认密码") @RequestParam(name = "pwd_confirm") String pwd_confirm
    ) throws Exception {
        iSysUserService.updatePwd(pwd_old, pwd_new, pwd_confirm);
        return JSONResult.ok();
    }
}
